from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2
import numpy as np


def grays_2(img,height,width,new_img1):
    # 图像量化等级为2的量化处理
    for i in range(height):
        for j in range(width):
            for k in range(3):  # 对应BGR三分量
                if img[i, j][k] < 128:
                    gray = 0
                else:
                    gray = 128
                new_img1[i, j][k] = np.uint8(gray)
    return new_img1


def grays_4(img,height,width,new_img2):
    # 图像量化等级为4的量化处理
    for i in range(height):
        for j in range(width):
            for k in range(3):  # 对应BGR三分量
                if img[i, j][k] < 64:
                    gray = 0
                elif img[i, j][k] < 128:
                    gray = 64
                elif img[i, j][k] < 192:
                    gray = 128
                else:
                    gray = 192
                new_img2[i, j][k] = np.uint8(gray)
    return new_img2


def grays_8(img,height,width,new_img3):
    # 图像量化等级为8的量化处理
    for i in range(height):
        for j in range(width):
            for k in range(3):  # 对应BGR三分量
                if img[i, j][k] < 32:
                    gray = 0
                elif img[i, j][k] < 64:
                    gray = 32
                elif img[i, j][k] < 96:
                    gray = 64
                elif img[i, j][k] < 128:
                    gray = 96
                elif img[i, j][k] < 160:
                    gray = 128
                elif img[i, j][k] < 192:
                    gray = 160
                elif img[i, j][k] < 224:
                    gray = 192
                else:
                    gray = 224
                new_img3[i, j][k] = np.uint8(gray)
    return new_img3


# 用户增加
@index_bp.get('/gravies_pro')
@index_bp.post('/gravies_pro')
def gravies_pro():
    if request.method == "GET":
        return render_template('admin/cv/gravies.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            choose = request.form.get('choose')
            img = cv2.imread(file_url)
            res_pic = "static/download/gravies.jpg"
            # 获取图像高度和宽度
            height = img.shape[0]
            width = img.shape[1]
            # 创建一幅图像
            new_img1 = np.zeros((height, width, 3), np.uint8)
            new_img2 = np.zeros((height, width, 3), np.uint8)
            new_img3 = np.zeros((height, width, 3), np.uint8)
            if choose == '0':
                res = grays_2(img,height,width,new_img1)
                cv2.imwrite(res_pic, res)
            elif choose == '1':
                res = grays_4(img, height, width, new_img2)
                cv2.imwrite(res_pic, res)
            elif choose == '2':
                res = grays_8(img, height, width, new_img3)
                cv2.imwrite(res_pic, res)
            return render_template('admin/cv/gravies.html',pic=file_url,res_pic=res_pic)
        else:
            return "only support jpg,png,gif"

